﻿using BaoShouTangServer.ApiModels;
using BaoShouTangServer.ApiServies;
using BaoShouTangServer.DTOModel;
using BaoShouTangServer.Models;
using BaoShouTangServer.Service;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using U8Login;

namespace BaoShouTangServer.Controllers
{
    public class RdispController : Controller
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // GET: Rdisp
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Rdisp(string docid)
        {
            Logger.Info($@"docid{docid}");
            HttpData<List<DTOU8材料出库单导入结果>> httpData = new HttpData<List<DTOU8材料出库单导入结果>>();

            Rdisp rdisp = SqlSugarHelp.scope.GetConnection("Rdisp").Queryable<Rdisp>().Where(it => it.docid == docid).First();
            if (rdisp == null)
            {
                httpData.msg = $"根据{docid}未查询到Rdisp数据";
                //返回提示
                return Json(httpData);
            }
            else
            {
                List<RDispatchLists> rDispatchLists = SqlSugarHelp.scope.GetConnection("Rdisp").Queryable<RDispatchLists>().Where(it => it.docid == docid).ToList();
                if (rDispatchLists == null || rDispatchLists.Count == 0)
                {
                    httpData.msg = $"根据docid:{docid}未查询到RDispatchLists数据";
                    //返回提示
                    return Json(httpData);
                }
                else
                {
                    string resp = "";
                    string msg = "";
                    string responseMsg = "";
                    clsLogin clsLogin = LogInService.GetLogin(ref resp, ref msg);
                    if (resp != "登陆成功")
                    {
                        return Json(new { success = false, message = $"{msg}" });
                    }
                    //根据rDispatchLists的dlid分组
                    var groupBy = rDispatchLists.GroupBy(it => it.DLID);
                    //遍历分组
                    foreach (var item in groupBy)
                    {
                        //获取dlid
                        int dlid = item.Key;
                        //获取该dlid下的所有发料记录
                        List<RDispatchLists> rDispatchList = item.ToList();

                        //查有么有生成过，生成过的就移除
                        foreach (var rDispatchLists1 in rDispatchList.ToList())
                        {
                            DispatchLists dispatch = SqlSugarHelp.scope.Queryable<DispatchLists>().Where(it => it.cDefine24 == rDispatchLists1.guid.ToString()).First();
                            if (dispatch != null)
                            {
                                DispatchList ds = SqlSugarHelp.scope.Queryable<DispatchList>().Where(it => it.DLID == dispatch.DLID).First();
                                httpData.msg += $"RDispatchLists.guid:{rDispatchLists1.guid.ToString()}已经生成过退货单，退货单号为:{ds?.cDLCode}。\r\n";
                                Logger.Info(httpData.msg);
                                //移除
                                rDispatchList.Remove(rDispatchLists1);
                            }
                        }
                        if (rDispatchList != null && rDispatchList.Count > 0)
                        {



                            Sales_FHD_T sales_FHD_T = SqlSugarHelp.scope.Queryable<Sales_FHD_T>().Where(it => it.dlid == dlid).First();
                            if (sales_FHD_T == null)
                            {
                                httpData.msg += $"根据docid:{docid},dlid:{dlid}未查询到Sales_FHD_T数据。\r\n";
                                return Json(httpData);
                            }
                            string 到货单接口返回result = "";
                            string Newdlid = "";
                            //调用生成退货单方法
                            string isSuccess = RdispService.生成退货单(clsLogin, sales_FHD_T, rDispatchList, ref Newdlid, ref 到货单接口返回result);
                            if (string.IsNullOrEmpty(isSuccess) == false)
                            {

                                httpData.msg += $"docid:{docid}.生成退货单失败,失败原因:{isSuccess}。\r\n";
                                Logger.Info(httpData.msg);
                            }
                            else
                            {
                                //跟新数据库
                                int.TryParse(Newdlid, out int id);
                                DispatchList ds = SqlSugarHelp.scope.Queryable<DispatchList>().Where(it => it.DLID == id).First();
                                foreach (var rDispatch in rDispatchList)
                                {
                                    rDispatch.isgen = true;
                                    rDispatch.U8退货单号 = ds?.cDLCode;
                                }
                              
                               
                                SqlSugarHelp.scope.GetConnection("Rdisp").Updateable(rDispatchList).WhereColumns(it => it.guid).ExecuteCommand();
                                httpData.msg += $"docid{docid}.生成退货单成功,退货单号为:{ds?.cDLCode},Newdlid:{Newdlid}。\r\n";
                                Logger.Info(httpData.msg);
                            }
                        }
             
                    }

                    return Json(httpData);

                }
            }
        }
    }
}